/*    file: main.cpp
 *    desc:
 * 
 * created: 2016-04-06
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include <stdio.h>
#include <iostream>

#include "BasicType.h"
#include "SharedPtr.h"
#include "TFileUtil.h"
#include "TByteBuffer.h"
#include "BasicMacro.h"
#include "TStringUtil.h"
#include <assert.h>
#include <errno.h>

#include "PsRecordCaster.h"
#include "PsCaster.h"

#include "DateTime.h"
#include "Path.h"


class Application : public av::PsRecordCasterSink
{
public:
	Application()
	{
		av::PsRecordCaster::init();

	}

	~Application()
	{
		av::PsRecordCaster::quit();
	}

	virtual void onCasterEvent(int channel, int event)
	{
		printf("onCasterEvent. chl: %d, event: %d\n", channel, event);
	}
};



void testQuery(av::PsRecordCaster& caster)
{
	printf("query record ... \n");

	av::MRecordQuery query = av::MRecordQuery();
	query.fromTime = 0;
	query.toTime   = 0;// fromTime + 24 * 60 * 60;

	av::MRecord records[av::MAX_RECORD];
	int count = caster.findRecord(query, 0, records, ARRAY_SIZE(records));
	for (int i = 0; i < count; i++)
	{
		printf("record. id:%d, chl:%s, duration:%d, name:%s\n",
			records[i].id, records[i].device, 
			records[i].duration, records[i].filepath);
	}

	if (count > 0)
	{
		caster.deleteRecord(records[0].id);
	}
	printf("\n");
}


int main(int argc, char** argv)
{
	std::string workdir = comn::Path::getWorkDir();
	//std::string filename = comn::Path::join(workdir, "MediaStorage.s3db");
	std::string filename = "G:\\PsCamera\\PsRecord\\vs\\DemoStorage\\MediaStorage.s3db";

	if (argc > 1)
	{
		filename = argv[1];
	}

	Application app;

	av::PsRecordCaster& caster = av::PsRecordCaster::instance();
	caster.setEventSink(&app);

	if (!caster.open(filename.c_str()))
	{
		printf("failed to open storage db. %s\n", filename.c_str());
		return 0;
	}

	//testQuery(caster);

	av::MRecord records[av::MAX_RECORD];
	av::MRecordQuery query = av::MRecordQuery();
	query.fromTime = 0;
	query.toTime = 0;// fromTime + 24 * 60 * 60;
	//query.device = "";

	int count = caster.findRecord(query, 0, records, av::MAX_RECORD);

	av::PsRecordCasterTransport transport = av::PsRecordCasterTransport();
	transport.protocol = PSCASTER_PROTOCOL_RTP;
	transport.esMode = 0;
	transport.payload = 96;
	transport.ssrc = 1;
	
	double scale = 1.0 / 1;
	int chl = caster.openChannelFile(transport, records, count, 0, scale);

	caster.addTarget(chl, "192.168.3.53", 10000);

	int64_t offset = 1000 * 70;
	caster.seek(chl, offset);

	caster.startCast(chl);

	while (true)
	{
		std::cout << "$";
		std::string line;
		std::getline(std::cin, line);

		if (line == "q")
		{
			break;
		}
		else if (line == "fast")
		{
			scale *= 2;
			caster.setScale(chl, scale);
		}
		else if (line == "slow")
		{
			scale = scale / 2;
			caster.setScale(chl, scale);
		}
		else if (line == "play")
		{
			caster.startCast(chl);
		}
		else if (line == "pause")
		{
			caster.pauseCast(chl);
		}
		else if (line == "seek")
		{
			int64_t offset = 1000 * 40;
			caster.seek(chl, offset);
		}
	}

	caster.closeChannel(chl);


	caster.close();

	return 0;
}




